package com.woniu.controller;

import com.woniu.entity.Emprole1;
import com.woniu.pojo.Emprole;
import com.woniu.pojo.Role;
import com.woniu.service.EmpRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("empRole")
public class EmpRoleController {

    @Autowired
    EmpRoleService empRoleService;

    /**
     * 跳转到分配员工角色页面
     * */
    @RequestMapping("formAssignEmpRole")
    public String formAssignEmpRole(){
        return "WEB-INF/emp/assignEmpRole";
    }

    /**
     * 查询已分配的角色
     * */
    @RequestMapping("findEmpRole")
    @ResponseBody
    public Map findIsRole(Integer empId){
        System.out.println("empId==="+empId);
        Map<String,Object> map= null;
        try {
            map = new HashMap<>();
            List<Emprole1> isEmpRole = empRoleService.findIsEmpRole(empId);
            List<Role> noEmpRole = empRoleService.findNoEmpRole(empId);
            map.put("isEmpRole", isEmpRole);
            map.put("noEmpRole", noEmpRole);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("error",e.getMessage());
            return map;
        }
        return map;
    }

    /**
     * 给emp添加角色
     * */
    @RequestMapping("add")
    @ResponseBody
    public String add(Emprole emprole){
        System.out.println(emprole);
        try {
            empRoleService.add(emprole);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "ok";
    }

    /**
     * 给emp取消角色
     * */
    @RequestMapping("deleteByExample")
    @ResponseBody
    public String deleteByExample(Emprole emprole){
        System.out.println(emprole);
        try {
            empRoleService.deleteByExample(emprole);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "ok";
    }

}
